package com.example.demoreptile;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.*;
import lombok.extern.slf4j.Slf4j;

/**
 * @version 1.0.0
 * @className: URlTest
 * @description:
 * @author: zhangjunfa
 * @date: 2023/7/17 15:17
 */
@Slf4j
public class URlTest {
    public static String baidu(String keyword) throws Exception {
        WebClient webclient = new WebClient();
        //ssl认证
        //webclient.getOptions().setUseInsecureSSL(true);
        //由于有的网页js书写不规范htmlunit会报错，所以去除这种错误让程序执行完全（不影响结果）
        webclient.getOptions().setThrowExceptionOnScriptError(false);
        webclient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        //不加载css
        webclient.getOptions().setCssEnabled(false);
        //由于是动态网页所以一定要加载js及执行
        webclient.getOptions().setJavaScriptEnabled(true);
        //打开百度高级搜索的网址
        HtmlPage htmlpage = webclient.getPage("http://www.baidu.com/gaoji/advanced.html");
        //获取网页from控件（f1为控件name）
        HtmlForm form = htmlpage.getFormByName("f1");
        HtmlSubmitInput button = form.getInputByValue("百度一下");
        HtmlTextInput textField = form.getInputByName("q1");
        textField.setValueAttribute(keyword);
        final HtmlSelect htmlSelet = form.getSelectByName("rn");
        htmlSelet.setDefaultValue("10");
        //隐藏值
        final HtmlHiddenInput hiddenInputtn = form.getInputByName("tn");
        hiddenInputtn.setDefaultValue("baiduadv");
        //发送请求（相当于点击百度一下按钮）获取返回后的网页
        final HtmlPage page = button.click();
        //获取网页的文本信息
        String result = page.asText();
        //获取网页源码
        //String result = page.asXml();
        //System.out.println(result);
        webclient.close();
        return result;
    }

    public static void main(String[] args) throws Exception {
        String baidu = baidu("会员");
        log.info("结果：{}", baidu);
    }

}
